package com.panshi.controller;

import com.panshi.entity.AppMatterCheck;
import com.panshi.service.MatterCheckService;
import com.panshi.tools.DataResult;
import com.panshi.tools.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 未审核问题controller
 */
@RestController
@Api(tags = "问题审核业务接口")
public class MatterCheckController {

    @Autowired
    private MatterCheckService matterCheckService;

    @Autowired
    private HttpServletRequest request;

    @ApiOperation("增加问题")
    @ApiImplicitParam(name = "appMatterCheck", value = "问题对象", required = true)
    @PostMapping("matter/add")
    public DataResult add(@RequestBody AppMatterCheck appMatterCheck){

        return matterCheckService.addMatter(appMatterCheck);
    }

    @ApiOperation("修改问题")
    @ApiImplicitParam(name = "appMatterCheck", value = "问题对象", required = true)
    @PostMapping("matter/update")
    public DataResult update(@RequestBody AppMatterCheck appMatterCheck){
        return matterCheckService.updateMatter(appMatterCheck);
    }

    @ApiOperation("删除问题")
    @ApiImplicitParam(name = "matterId", value = "问题id", required = true)
    @GetMapping("matter/delete/{matterId}")
    public DataResult delete(@PathVariable Integer matterId){
        return matterCheckService.deleteMatter(matterId);
    }


    @ApiOperation("审核/上架/下架")
    @ApiImplicitParam(name = "appMatterCheck", value = "未审核问题对象", required = true)
    @PostMapping("matter/adopt")
    public DataResult adopt(AppMatterCheck appMatterCheck){
        System.out.println(appMatterCheck);
        return matterCheckService.adoptCheck(appMatterCheck);
    }

    @ApiOperation("根据用户id查询问题")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    @GetMapping("matter/user/{userId}") // http://localhost:1001/matter/user/1?pageNumber=1&pageSize=10
    //@HystrixCommand(fallbackMethod = "queryGet")
    public DataResult queryByUid(@PathVariable Integer userId, PageBean pageBean){
        return matterCheckService.queryByUid(userId, pageBean);
    }

    @GetMapping("matter/c-info/{matterId}")
    public AppMatterCheck queryInfo(@PathVariable Integer matterId){
        return matterCheckService.queryInfo(matterId);
    }

    @GetMapping(value = {"matter/query","matter/query/{classifyId}"})
    @ApiImplicitParam(name = "classifyId", value = "分类id", required = true)
    public DataResult query(@PathVariable(required = false) Integer classifyId){
        PageBean pageBean = new PageBean();
        pageBean.setPageNumber(Integer.valueOf(request.getParameter("page")));
        pageBean.setPageSize(Integer.valueOf(request.getParameter("limit")));
        return matterCheckService.query(classifyId,pageBean);
    }

    @ApiOperation("查询未审核问题")
    @GetMapping("matter/uncheck")
    public DataResult queryUnCheck(){
        PageBean pageBean = new PageBean();
        pageBean.setPageNumber(Integer.valueOf(request.getParameter("page")));
        pageBean.setPageSize(Integer.valueOf(request.getParameter("limit")));
        return matterCheckService.queryUnCheck(pageBean);
    }

    // 熔断方法
    public DataResult queryGet(@PathVariable Integer userId, PageBean pageBean){
        return new DataResult().setCode(500).setMsg("用户id => " + userId +" 不存在，没有对应的数据，请重试~" );
    }

}
